﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmDanhMucLoaiKhach : Form
    {
        public frmDanhMucLoaiKhach()
        {
            InitializeComponent();
        }

        LoaiKhachDTO lkDTO = null;
        Status TrangThai = Status.Xem;

        //Hiển thị lên DataGripView
        private void LoadData()
        {
            loaiKhachDTOBindingSource.DataSource = LoaiKhachBUS.SelectAll();
            loaiKhachDTODataGridView.Enabled = true;

            btnLoaiKhachMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }
        private void AnHienTxt(Boolean t)
        {
            txtTenLoaiKhach.ReadOnly = t;
            txtGhiChu.ReadOnly = t;
        }
        private void frmDanhMucLoaiKhach_Load(object sender, EventArgs e)
        {
            LoadData();

            AnHienTxt(true);
        }

        private void btnLoaiKhachMoi_Click(object sender, EventArgs e)
        {
            lkDTO = loaiKhachDTOBindingSource.AddNew() as LoaiKhachDTO;
            TrangThai = Status.Them;

            loaiKhachDTODataGridView.Enabled = false;
            btnLoaiKhachMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;

            AnHienTxt(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            lkDTO.TenLoaiKhach = txtTenLoaiKhach.Text;
            lkDTO.GhiChu = txtGhiChu.Text;
            string strKQ = null;
            if (TrangThai == Status.Them)
            {
                strKQ = LoaiKhachBUS.Them(lkDTO);
                if (strKQ == "True")
                {
                    lkDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }

            if (TrangThai == Status.Sua)
            {
                strKQ = LoaiKhachBUS.CapNhat(lkDTO);
                if (strKQ == "True")
                {
                    lkDTO = null;
                    TrangThai = Status.Xem;
                    LoadData();
                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            lkDTO = loaiKhachDTOBindingSource.Current as LoaiKhachDTO;
            TrangThai = Status.Sua;

            loaiKhachDTODataGridView.Enabled = false;
            btnLoaiKhachMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            lkDTO = loaiKhachDTOBindingSource.Current as LoaiKhachDTO;
            if (MessageBox.Show("Bạn có chắc xóa", "Thông tin", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                strKQ = LoaiKhachBUS.Xoa(lkDTO);
                if (strKQ == "True")
                {
                    loaiKhachDTOBindingSource.RemoveCurrent();

                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }
    }
}
